<?php
require_once dirname(__DIR__) . '/define.php';
ini_set("memory_limit", "2G");

defined('APPLICATION_PATH') || define('APPLICATION_PATH', dirname(__DIR__) . '/application');
defined('APPLICATION_ROOT') || define('APPLICATION_ROOT', dirname(APPLICATION_PATH));

set_include_path(implode(PATH_SEPARATOR, array(
    APPLICATION_ROOT,
    APPLICATION_ROOT . '/library',
    APPLICATION_ROOT . '/library/',
    get_include_path()
)));

// set global functions
//require_once APPLICATION_ROOT . '/library/error_handler.php';
// try to get environment option from command line
$environment = null;
foreach ($argv as $arg) {
    if (preg_match('/--env=(\w+)/', $arg, $result)) {
        $environment = $result[1];
    }
}
// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', ($environment ? $environment : (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'live')));
unset($environment);


require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Ops_')
       ->registerNamespace('Zendvn_')
;

$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini');


$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->bootstrap();

Zend_Controller_Front::getInstance()
                     ->setRequest(new Zendvn_Controller_Request_Cli())
                     ->setResponse(new Zendvn_Controller_Response_Cli())
                     ->setRouter(new Zendvn_Controller_Router_Cli())
                     ->setParam('noViewRenderer', TRUE)
                     ->setControllerDirectory(array('default' => APPLICATION_PATH . '/modules/default/controllers'))
;

Zend_Registry::set('config', $config);
$application->run();
